package top.codetheory._01_动态数组.leetcode;

/**
 * @author:
 * @since: 2023/10/30 17:58
 **/
public class _34_在排序数组中查找元素的第一个和最后一个位置 {
    public static void main(String[] args) {
        int[] nums = new int[]{5, 7, 7, 8, 8, 10};
        int target = 8;
        int[] ints = searchRange(nums, target);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public static int[] searchRange(int[] nums, int target) {
        if (nums == null || nums.length == 0) return new int[]{-1, -1};
        int left = 0, right = nums.length - 1, middle;
        while (left <= right) {
            middle = (left + right) >> 1;
            if (target < nums[middle]) {
                right = middle - 1;
            } else if (nums[middle] < target) {
                left = middle + 1;
            } else {
                return new int[]{left, left + 1};
            }
        }
        return new int[]{-1, -1};
    }
}